package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runing.config.ResponseWrapper;
import com.runing.entity.MVersion;
import com.runing.entity.RClasses;
import com.runing.entity.RStatistic;
import com.runing.entity.RStudent;
import com.runing.mapper.MVersionMapper;
import com.runing.mapper.RStudentMapper;
import com.runing.service.MVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-13
 */
@Service
public class MVersionServiceImpl extends ServiceImpl<MVersionMapper, MVersion> implements MVersionService {

    @Autowired
    MVersionMapper versionMapper;
    @Autowired
    RStudentMapper studentMapper;

    public ResponseWrapper addVersion(MVersion version){
        QueryWrapper<MVersion> mVersionQueryWrapper = new QueryWrapper<>();
        mVersionQueryWrapper.eq("snum",version.getSnum());
        MVersion mVersion = versionMapper.selectOne(mVersionQueryWrapper);
        if (mVersion != null){
            version.setMId(mVersion.getMId());
            versionMapper.updateById(version);
            if (version.getMCategory().equals("devTools") && version.getIsmobile()==false ){
                QueryWrapper<RStudent> studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper.eq("snum",version.getSnum());
                RStudent student = studentMapper.selectOne(studentQueryWrapper);
                if (student == null){
                    return ResponseWrapper.markCustom(false,200,"没找到该用户",null);
                }else {
                    student.setSdelete(true);
                    studentMapper.updateById(student);
                    return ResponseWrapper.markCustom(true,200,"您的账号已封禁，请联系辅导员",null);
                }
            }else {
                return ResponseWrapper.markSuccess(null);
            }

        }else {
            if (version.getMCategory().equals("devTools") && version.getIsmobile()==false ){
                QueryWrapper<RStudent> studentQueryWrapper = new QueryWrapper<>();
                studentQueryWrapper.eq("snum",version.getSnum());
                RStudent student = studentMapper.selectOne(studentQueryWrapper);
                if (student == null){
                    return ResponseWrapper.markCustom(false,200,"没找到该用户",null);
                }else {
                    versionMapper.insert(version);
                    student.setSdelete(true);
                    studentMapper.updateById(student);
                    return ResponseWrapper.markCustom(true,200,"您的账号已封禁，请联系辅导员",null);
                }
            }else {
                return ResponseWrapper.markSuccess(null);
            }
        }
    }

    public void verExcel(HttpServletResponse response)throws IOException {
        //确定编码的格式
        response.setCharacterEncoding("UTF-8");
        //获取导出的数据

        List<Map> list = versionMapper.allStu();
        System.out.println(list);
        //创建excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        //创建sheet页
        XSSFSheet sheet = wb.createSheet("学生信息表");
        //创建标题行
        XSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("学生姓名");
        titleRow.createCell(1).setCellValue("班级");
        titleRow.createCell(2).setCellValue("学院");
        titleRow.createCell(3).setCellValue("学号");
        titleRow.createCell(4).setCellValue("手机类型");
        titleRow.createCell(5).setCellValue("系统型号");

        //遍历将数据放到excel列中
        for (Map student : list) {
            XSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
            dataRow.createCell(0).setCellValue(String.valueOf(student.get("sname")));
            dataRow.createCell(1).setCellValue(String.valueOf(student.get("clname")));
            dataRow.createCell(2).setCellValue(String.valueOf(student.get("cname")));
            dataRow.createCell(3).setCellValue(String.valueOf(student.get("snum")));
            if (student.get("m_category").equals("devTools")){
                dataRow.createCell(4).setCellValue("使用模拟器作弊");
            }else {
                dataRow.createCell(4).setCellValue(String.valueOf(student.get("m_category")));
            }
            dataRow.createCell(5).setCellValue(String.valueOf(student.get("m_version")));
        }
            /*   // 设置下载时客户端Excel的名称
            String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls";
               response.setContentType("application/vnd.ms-excel");
               response.setHeader("Content-disposition", "attachment;filename=" + filename);  */

        // 设置下载时客户端Excel的名称   （上面注释的改进版本，上面的中文不支持）
        response.setContentType("application/octet-stream;charset=utf-8");
        String name = "学生手机版本号";
        response.setHeader("Content-Disposition", "attachment;filename="
                //该版本为2007-的版本
                + new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)
                + ".xlsx");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

    }

}
